package demo3;

import java.util.ArrayList;
import java.util.Scanner;

public class FoodOperation {
    private static ArrayList<Food> foods = new ArrayList<>();

    public static void addFood(){
        Scanner sc = new Scanner(System.in);
        Food food = new Food();
        System.out.println("请输入菜品名：");
        String name = sc.next();
        food.setName(name);
        System.out.println("请输入价格：");
        double price = sc.nextDouble();
        food.setPrice(price);
        System.out.println("请输入该菜品的评价：");
        String desc = sc.next();
        food.setDesc(desc);
        if ( foods.add(food)){
            System.out.println("添加成功~");
        }
        else {
            System.out.println("添加失败~");
        }
    }
    public static void showAllFoods(){
        if (foods.size() == 0){
            System.out.println("您还没有添加商品，请先添加了，再查询");
        }
        else {
            for (int i = 0; i < foods.size(); i++) {
                System.out.println(i + 1 + ".name: " + foods.get(i).getName());
                System.out.println(i + 1 + ".price: " + foods.get(i).getPrice());
                System.out.println(i + 1 + ".desc: " + foods.get(i).getDesc());
            }
        }
    }
    public static void findFoodByName(String name){
        boolean flag = false;
        for (int i = 0; i < foods.size(); i++) {
            if (foods.get(i).getName().equals(name)){
                System.out.println("name: " + foods.get(i).getName());
                System.out.println("price: " + foods.get(i).getPrice());
                System.out.println("desc: " + foods.get(i).getDesc());
                flag = true;
                break;
            }
        }
        if (!flag) System.out.println("你输入的菜名，在系统重不存在");
    }

    public static void start(){
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请选择要操作的选项：");
            System.out.println("======1.添加菜品 ======");
            System.out.println("======2.查询全部的菜品 ======");
            System.out.println("======3.根据名称查询菜品 ======");
            System.out.println("======4.退出 ======");
            int command = sc.nextInt();
            switch(command){
                case 1:
                    addFood();
                    break;
                case 2:
                    showAllFoods();
                    break;
                case 3:
                    System.out.println("请输入要查询的菜品名称：");
                    String name = sc.next();
                    findFoodByName(name);
                    break;
                case 4:
                    return;
                default:
                    System.out.println("输入错误，请重新输入！！！");
            }
        }
    }

}
